import random

def get_adj_list(fname):
    adjs = []    
    with open(fname, 'r') as adj_fin:
        adjs = [x.strip() for x in adj_fin]
    return adjs

def get_noun_list(fname):
    nouns = []
    with open('nouns.txt', 'r') as noun_fin:
        nouns = [x.strip() for x in noun_fin]
    return nouns

def make_random_pair(adjs, nouns):
    a = random.choice(adjs)
    n = random.choice(nouns)
    return " ".join([a, n])

adjs = get_adj_list("adjectives.txt")
nouns = get_noun_list("nouns.txt")

def ttl_trial(adjs, nouns, lower, higher):
    used = []
    ttl = random.randint(lower, higher)
    fail = 0

    for i in xrange(ttl - 1):
        print("\n" * 10)
        print(i + 1)
        pair = make_random_pair(adjs, nouns)
        print(pair[0] + " " + pair[1])
        used.append(pair)
        choice = raw_input("(n)ew or (o)ld? ")
        while choice[0] not in 'no':
            choice = raw_input("(n)ew or (o)ld? ")
        if choice != 'n':
            print("No, that was new!")
            fail += 1

    print("\n" * 10)
    print(ttl)    
    pair = random.choice(used)
    print(pair[0] + " " + pair[1])    
    choice = raw_input("(n)ew or (o)ld? ")
    while choice[0] not in 'no':
        choice = raw_input("(n)ew or (o)ld? ")
    if choice != 'o':
        print("No, that was old!")
        fail += 1
    else:
        print("Correct")

    print("Failed %d" % fail)

def fraction_trial(generate, fraction, count, at_least):
    NEW = 0
    OLD = 1
    OPTIONS = 'no'
    used = []
    fail = [0, 0]
    total = [0, 0]

    for i in xrange(count):
        print("\n" * 10)
        print(i + 1)
        item = None
        which = NEW
        rand = random.random()
        if i >= at_least and rand <= fraction:
            item = random.choice(used)
            which = OLD
        else:
            item = generate()
            while item in used:
                item = generate()
            used.append(item)
        total[which] += 1            
        print(item)
        choice = raw_input("(n)ew or (o)ld? ")
        while choice[0] not in OPTIONS:
            choice = raw_input("(n)ew or (o)ld? ")
        if choice[0] != OPTIONS[which]:
            raw_input("Incorrect!")
            fail[which] += 1

    print("%d trials; new (%d/%d); old (%d/%d)" % (count, total[NEW] - fail[NEW], total[NEW], total[OLD] - fail[OLD], total[OLD]))   

fraction_trial(lambda: make_random_pair(adjs, nouns), 0.1, 200, 20)
#fraction_trial(lambda: random.randint(100, 999), 0.1, 200, 20)
